package com.liang.book.controller;

import com.liang.book.constant.Constant;
import com.liang.book.model.CaptchaProperties;
import com.liang.book.model.Result;
import com.liang.book.model.Roles;
import com.liang.book.model.UserInfo;
import com.liang.book.service.CaptchaService;
import com.liang.book.service.UserService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private CaptchaService captchaService;

    @RequestMapping(value = "/login", produces = "application/json")
    public Result<String> login(String userName, @RequestParam("password") String passWord,
                                HttpSession session) {
        log.info("用户登录 username：{}, password:{}, captcha:{}", userName, passWord);

        //1.参数校验
        if (!StringUtils.hasLength(userName) || !StringUtils.hasLength(passWord)) {
            return Result.fail("用户名或密码不能为空");
        }

        //3.验证用户名和密码
        UserInfo userInfo = userService.queryUserByName(userName);
        if (userInfo == null || userInfo.getId() < 0) {
            return Result.fail("用户名不存在");
        }
        if (!passWord.equals(userInfo.getPassWord())) {
            return Result.fail("账号或密码错误");
        }

        // 4.存session 默认存在内存中
        userInfo.setPassWord("");//存在服务器端的
        session.setAttribute(Constant.USER_SESSION_KEY, userInfo);
        session.setAttribute("userName", userName);
//        System.out.println(session.getAttribute("userName"));
        String description = userService.findRole(userName).getDescription();
        System.out.println(description);
        session.setAttribute("role", description);
        System.out.println("得到属性" + session.getAttribute("role"));
        return Result.success(description);
    }


//    @RequestMapping("/getRole")
//    public String getRole() {
//
//    }


    @RequestMapping(value = "/register", produces = "application/json")
    public Result<String> register(String userName, @RequestParam("password") String passWord, String confirmPassword,
                                   HttpSession session, String realName, String phone,Integer role) {
        log.info("用户注册 username：{}, password:{}", userName, passWord);
        //1.参数校验
        if (!StringUtils.hasLength(userName) || !StringUtils.hasLength(passWord)) {
            return Result.fail("用户名或密码不能为空");
        }
        if (!StringUtils.hasLength(confirmPassword)) {
            return Result.fail("确认密码不能为空");
        }
        if (!StringUtils.hasLength(realName)) {
            return Result.fail("真实姓名不能为空");
        }
        if (!StringUtils.hasLength(phone)) {
            return Result.fail("手机号码不能为空");
        }

        // 2. 验证用户名是否已存在
        UserInfo userInfo = userService.queryUserByName(userName);
        if (userInfo != null) {
            return Result.fail("用户名已存在");
        }

        //
        System.out.println("接收到role"+role);
        String  description = " ";
        if(role == 2) {
            description = "admin";
        }
        if(role == 1) {
            description = "user";
        }
        System.out.println(" description: " +  description);


        // 3. 创建用户信息并保存到数据库
        UserInfo newUser = new UserInfo();
        newUser.setUserName(userName);
        newUser.setPassWord(passWord);
        newUser.setRealName(realName);
        newUser.setPhone(phone);
        newUser.setRole(description);// 实际应用中应该加密存储密码
        System.out.println("newUser:" + newUser);

        // 调用userService的注册方法保存用户信息
        boolean registerSuccess = userService.registerUser(newUser);
        System.out.println(" registerSuccess:" + registerSuccess);
        if (!registerSuccess) {
            return Result.fail("注册失败，请稍后再试");
        }
//        String description = userService.findRole(userName).getDescription();
        // 返回注册成功的结果
        return Result.success("注册成功");
    }

    @RequestMapping("/findRole")
    public Roles findRole(String userName) {
        return userService.findRole(userName);
    }


    @RequestMapping("/selectAdminInfo")
    public UserInfo selectAdminInfo(String userName) {
        return userService.selectAdminInfo(userName);
    }


}